# -*- coding:utf-8 -*-
from flask import request, jsonify, abort
from flask_cors import CORS
from com.zzh.dao_v2.hotDao import HotDao

from . import app

CORS(app, supports_credentials=True)


@app.route('/insertHot', methods=['POST'])
def insert_hot():
    """
    添加信息
    :return:
    """
    if not request.json:
        abort(400)
    info = {
        'hotName': request.json.get('hotName', ''),
        'hotId': request.json.get('hotId', ''),
        'hotType': request.json.get('hotType', '')
    }

    info = HotDao().insertHot(info)
    if info:
        return jsonify({"message": {"code": 1, "status": "success"}})
    else:
        return jsonify({"message": {"code": 0, "status": "error"}})


@app.route('/findHotById', methods=['POST'])
def find_hot_by_id():
    """
    根据ID查询
    :return:
    """
    if not request.json:
        abort(400)
    hotId = request.json.get('hotId', '')
    info = HotDao().findHotById(hotId)

    if info:
        return jsonify({"info": info, "message": {"code": 1, "status": "success"}})
    else:
        return jsonify({"message": {"code": 0, "status": "error"}})


@app.route('/findHotByCondition', methods=['POST'])
def find_hot_by_condition():
    """
    条件查询
    :return:
    """
    if not request.json:
        abort(400)

    try:
        page = request.json['page']
        pageIndex = page["pageIndex"]
        pageSize = page["pageSize"]
    except:
        return jsonify({"message": {"code": 0, "status": "请传入 pageIndex和pageSize"}})

    try:
        conditions = request.json['condition']
    except:
        return jsonify({"message": {"code": 0, "status": "未传入指定condition"}})
    try:
        multiSort = request.json['multiSort']
    except:
        return jsonify({"message": "未传入排序规则multiSort"})

    info, total = HotDao().findHotByConditon(pageIndex=pageIndex, pageSize=pageSize, conditions=conditions, multiSort=multiSort)

    if info:
        return jsonify({"total": total,  "info": info, "message": {"code": 1, "status": "success"}})
    else:
        return jsonify({"message": {"code": 0, "status": "error"}})


@app.route('/deleteHotByIds', methods=['POST'])
def delete_hot_by_ids():
    """
    根据ID删除，ID以数组形式传递
    :return:
    """
    if not request.json:
        abort(400)
    idArray = request.json.get('idArray', [0])

    info = HotDao().deleteHot(idArray)
    if info:
        return jsonify({"message": {"code": 1, "status": "success"}})
    else:
        return jsonify({"message": {"code": 0, "status": "error"}})


@app.route('/updateHotById', methods=['POST'])
def update_hot_by_id():
    """
    更新
    :return:
    """
    if not request.json:
        abort(400)
    try:
        id = request.json['id']
    except:
        return jsonify({"message": {"code": 0, "status": "请传入要修改信息的ID"}})
    oldInfo = HotDao().selectHot(id)
    info = {
        'id': request.json['id'],
        'hotName': request.json.get('hotName', oldInfo[1]),
        'hotId': request.json.get('hotId', oldInfo[2]),
        'hotType': request.json.get('hotType', oldInfo[3])
    }

    info = HotDao().updateHot(info)
    if info:
        return jsonify({"info": info, "message": {"code": 1, "status": "success"}})
    else:
        return jsonify({"message": {"code": 0, "status": "error"}})


@app.route('/hotSearch', methods=['GET'])
def hotSearch():
    # 换一换，随机返回一条数据
    info = HotDao().getRandomItem()
    if info:
        return jsonify({"info": info, "message": {"code": 1, "status": "success"}})
    else:
        return jsonify({"message": {"code": 0, "status": "暂时没有符合条件的数据可展示"}})